package com.pw.study.flink.transform;

import com.pw.study.flink.entities.WaterSensor;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.streaming.api.functions.ProcessFunction;
import org.apache.flink.util.Collector;

import java.util.ArrayList;

public class TransFormProcess {
    public static void main(String[] args) {
        //dealProcess();
        dealProcess2();
    }
    private static void dealProcess2() {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 20000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(3);

        ArrayList<WaterSensor> waterSensors = new ArrayList<>();
        waterSensors.add(new WaterSensor("sensor_1", 1607527992000L, 20));
        waterSensors.add(new WaterSensor("sensor_1", 1607527994000L, 50));
        waterSensors.add(new WaterSensor("sensor_1", 1607527996000L, 50));
        waterSensors.add(new WaterSensor("sensor_2", 1607527993000L, 10));
        waterSensors.add(new WaterSensor("sensor_2", 1607527995000L, 30));
        env.fromCollection(waterSensors).keyBy(WaterSensor::getId).process(new KeyedProcessFunction<String, WaterSensor, Tuple2<String, Integer>>() {
            @Override
            public void processElement(WaterSensor sensor, Context ctx, Collector<Tuple2<String, Integer>> out) throws Exception {
                System.out.println(sensor);
                System.out.println("------------------"+ctx.getCurrentKey());
                out.collect(new Tuple2<>(ctx.getCurrentKey(), sensor.getVc()));
            }
        }).print();


        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void dealProcess() {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 20000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(3);
        DataStreamSource<Integer> stream = env.fromElements(1, 2, 3, 4, 5, 6);
        ArrayList<WaterSensor> waterSensors = new ArrayList<>();
        waterSensors.add(new WaterSensor("sensor_1", 1607527992000L, 20));
        waterSensors.add(new WaterSensor("sensor_1", 1607527994000L, 50));
        waterSensors.add(new WaterSensor("sensor_1", 1607527996000L, 50));
        waterSensors.add(new WaterSensor("sensor_2", 1607527993000L, 10));
        waterSensors.add(new WaterSensor("sensor_2", 1607527995000L, 30));
        env.fromCollection(waterSensors).process(new ProcessFunction<WaterSensor, Tuple2<String, Integer>>() {
            @Override
            public void processElement(WaterSensor sensor, Context ctx, Collector<Tuple2<String, Integer>> out) throws Exception {
                System.out.println(sensor);
                out.collect(new Tuple2<>(sensor.getId(), sensor.getVc()));
            }
        }).print();


        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }



}
